package cc.comac.mom.queue;


import java.util.concurrent.LinkedBlockingQueue;

import cc.comac.mom.bean.BusLog;
import cc.comac.mom.bean.LogConstant;
import cc.comac.mom.util.ConfigUtil;


/**
    * @ClassName: BusLogQueue
    * @Description: 业务日志队列
    * @author Administrator
    * @date 2018年8月29日
    *
    */

public class BusLogQueue {

	//队列大小
//    public static final int QUEUE_MAX_SIZE  = Integer.parseInt(ConfigUtil.getValue(LogConstant.K_QUEUE_SIZE));
    public static final int QUEUE_MAX_SIZE  = -1;

    private static BusLogQueue alarmMessageQueue = new BusLogQueue();

    //阻塞队列
//    private LinkedBlockingQueue<BusLog> blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE==-1?Integer.MAX_VALUE:QUEUE_MAX_SIZE);
    private LinkedBlockingQueue<BusLog> blockingQueue = new LinkedBlockingQueue<>(Integer.MAX_VALUE);

    private BusLogQueue(){}
    public static BusLogQueue getInstance() {
        return alarmMessageQueue;
    }
    /**
     * 消息入队
     * @param salesLog
     * @return
     */
    public boolean push(BusLog salesLog) {
        return this.blockingQueue.add(salesLog);//队列满了就抛出异常，不阻塞
    }
    
    /**
     * 消息出队
     * @return
     */
    public BusLog poll() {
    	BusLog result = null;
        try {
            result = this.blockingQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取队列大小
     * @return
     */
    public int size() {
        return this.blockingQueue.size();
    }
}
